<?php

	namespace Aspic\Database\Sqlite;

use Aspic as A;

	/**
	 * 
	 * @author Mallory Dessaintes
	 */
	class Database extends A\Database\AbstractDatabase {
		protected $_connString = 'sqlite:{path}';

		public function __construct($path) {
			parent::__construct();

			$pdoReflection = new \ReflectionClass('PDO');

			$dbInfos = array(
				'path' => $path, 
			);
			
			$this->initPdo($dbInfos);
		}
		
		protected function initPdo(array $dbInfos) {
            $pdoReflection = new \ReflectionClass('PDO');
            
            $this->_dbInfos = $dbInfos;

			$connectionString = A\UString::fromArgsArray($this->_connString, $dbInfos);

			$pdoArgs = array(
				$connectionString,
				A\Util::getIfSet($dbInfos['username']),
				A\Util::getIfSet($dbInfos['password']),
			);

			$this->_pdo = $pdoReflection->newInstanceArgs($pdoArgs);
        }
		
		public function fetchTablesNames() {
			throw new A\NotImplementedException;
		}
		
		public function fetchTableColumns($tableName) {
			$sql = 'PRAGMA table_info(`'.$tableName.'`)';
			$cols = $this->_db->getResults($sql);
			
			$res = array();
			
			foreach ($cols as $col) {
				$res[] = array(
					'name' => $col['name'],
					'type' => $col['type'],
					'null' => !((bool)$col['notnull']),
					'default' => !((bool)$col['default']),
					'pk' => $col['pk'],
				);
			}
			
			return $res;
		}
		
		public function fetchForeignsKeys() {
			throw new A\NotImplementedException;
		}

	}

?>
